import axios from 'axios';
import { FormSchema } from '/@/components/Form';
import { useGlobSetting } from '/@/hooks/setting';
import { getToken } from '/@/utils/auth';
import { getCategoryList, createBook, createContents } from '/@/api/book/book';

const languageOptions: LabelValueOptions = [
  {
    label: 'EN',
    value: 'en',
  },
  {
    label: 'ZH',
    value: 'zh',
  },
];

let categoryOptions: LabelValueOptions = [
  // {
  //   label: '前端',
  //   value: '1',
  // },
];

export const categoryOptionList = async (updateSchema) => {
  categoryOptions = []
  const data = await getCategoryList()
  // console.log("🚀 ~ categoryOptionList ~ data:", data)
  if (data.length > 0) {
    data.map(item => {
      categoryOptions.push({label: item.name, value: item.id})
    })
  }
  // console.log("🚀 ~ categoryOptionList ~ categoryOptions:", categoryOptions)
  updateSchema(
    {
      field: 'category',
      component: 'Select',
      label: '类别',
      componentProps: {
        options: categoryOptions,
      },
      colProps: {
        offset: 2,
      },
      required: true,
    }
  )
};

export const schemas: FormSchema[] = [
  {
    field: 'title',
    component: 'Input',
    label: '书名',
    required: true,
  },
  {
    field: 'author',
    component: 'Input',
    label: '作者',
    required: true,
    // componentProps: {
    //   addonBefore: 'http://',
    //   addonAfter: 'com',
    // },
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'publisher',
    component: 'Input',
    label: '出版社',
    required: true,
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'language',
    component: 'Select',
    label: '语言',
    componentProps: {
      options: languageOptions,
    },
    required: true,
    // colProps: {
    //   offset: 2,
    // },
  },
  {
    field: 'category',
    component: 'Select',
    label: '类别',
    componentProps: {
      options: categoryOptions,
    },
    colProps: {
      offset: 2,
    },
    required: true,
  },
  {
    field: 'fileName',
    component: 'Input',
    label: '文件名',
    componentProps: {
      disabled: true,
    },
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'cover',
    component: 'Input',
    label: '封面路径',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'rootFile',
    component: 'Input',
    label: '根文件地址',
    componentProps: {
      disabled: true,
    },
    colProps: {
      offset: 2,
    },
  },
];

export const taskSchemas = ({ setFieldsValue, contentData }): FormSchema[] =>{
  return [
    {
      field: 'book',
      component: 'Upload',
      label: '电子书',
      required: true,
      componentProps: {
        maxSize: 20,
        maxNumber: 1,
        accept: ['epub'],
        api: (data) => {
          // console.log("🚀 ~ data:", data)
          const formData = new FormData();
          formData.append('file', data.file, encodeURIComponent(data.file.name));
          const { apiUrl } = useGlobSetting();
          const requestUrl = `${apiUrl}/book/upload`;
          // console.log("🚀 ~ requestUrl:", requestUrl)
          return axios.post(requestUrl, formData, {
            headers: {
              'Content-Type': data.file.type,
              'Authorization': `Bearer ${getToken()}`
            }
          })
          // return 'http://localhost:3000/api/v1/book/upload'
        },
        onChange(files) {
          console.log("🚀 ~ onChange files:", files)
          console.log("🚀 ~ onChange:", setFieldsValue)
          if (!files || files.length == 0) return;
          if (!files[0].data) return;
          console.log("🚀 ~ onChange files[0]:", files[0].data)
          let { title, creator, publisher, language, cover, rootFile, content } = files[0].data
          setFieldsValue({
            title,
            author: creator,
            publisher,
            language,
            fileName: files[0].originalname,
            cover,
            rootFile,
          })
          contentData.value = content
          console.log("🚀 ~ onChange ~ contentData:", contentData.value)
          
        } 
      }
    },
  ]
};

export const addBook = async (val) => {
  return createBook({...val, category: parseInt(val.category) })
}

export const addContents = async (val) => {
  console.log("🚀 ~ addContents ~ val:", val.book[0].data.content)
  // return createContents({...val })
}
